package com.maowei.config;

import com.alibaba.otter.canal.client.CanalConnector;
import com.alibaba.otter.canal.client.CanalConnectors;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.InetSocketAddress;

/**
 * canal 客户端连接配置
 * @author maowei
 * @date 2021/8/24
 */
@Configuration
@ConditionalOnClass(CanalConnectors.class)
public class CanalConnectorConfiguration {

    private final CanalConfigProperties canalConfigProperties;

    public CanalConnectorConfiguration(CanalConfigProperties canalConfigProperties) {
        this.canalConfigProperties = canalConfigProperties;
    }

    @Bean
    public CanalConnector initConnector(){
        CanalConnector connector = CanalConnectors.newSingleConnector(new InetSocketAddress(canalConfigProperties.getHost(),
                canalConfigProperties.getPort()), canalConfigProperties.getDestination(), canalConfigProperties.getUserName(), canalConfigProperties.getPassword());
        connector.connect();
        connector.subscribe(canalConfigProperties.getFilter());
        connector.rollback();
        return connector;
    }

}
